<?

//require_once(dirname(__FILE__) . '/../abstract/inet.php');

class bf_inet_externalip_net extends bf_inet {

	private $server_address_cached;
	private $log;

	public function __construct($options = array()) {

		$this->opts = array_merge(array(
			'api' => 'http://api.externalip.net/ip/',
			'method' => 'GET',
			'params' => array(),
			'auth' => array(),
			'log' => dirname(__FILE__) . '/../../../logs/externalip.net.log',
			'logging' => 2,
		), $options);

		$this->server_address_cached = false;
		$this->log = new bf_log($this->opts['log']);
	}

	public function server_address() {
		if (!$this->server_address_cached) {
			$r = (
				$this->opts['method'] == 'GET'
				? bf::$http->get($this->opts['api'], $this->opts['params'], $this->opts['auth'])
				: bf::$http->post($this->opts['api'], $this->opts['params'], $this->opts['auth'])
			);
			if ($this->opts['logging'] >= 2) {
				$this->log->append(
					"------------------------------\n" .
					"Date: " . date('Y-m-d H:i:s') . "\n" .
					"Server name: " . @$_SERVER['SERVER_NAME'] . "\n" .
					"Request: " . @$_SERVER['REQUEST_URI'] . "\n\n" .
					"Status: {$r['status']}\n" .
					"Response:\n{$r['response']}\n"
				);
			}
			if ($r['status'] == '200') {
				$this->server_address_cached = trim($r['response']);
				if ($this->opts['logging'] == 1)
					$this->log->append(date('Y-m-d H:i:s') . " {$this->server_address_cached}\n");
			}
		}
		return $this->server_address_cached;
	}
}

?>